Aloha!又是我少女人妻 Uerica!跟大家閒聊廢話的時間只剩下七天了,以後有廢話不知道去哪說了哈哈哈。這幾天發生了一些小不順遂的事,但每次我都會把手放在自己胸口默念:凡事發生必有其目的,並有助於我。有時候就這樣念到睡著,總之停止抱怨心情就會好很多了,希望我鑰匙趕快出現阿~哈哈哈!
用條件過濾欄位值的結果
使用欄位類型需遵守以下表格
Type | Value Filter | Range Filter | Geo Filter |
---|---|---|---|
text | Yes | No | No |
number | Yes | Yes | No |
date | Yes | Yes | No |
geolocation | Yes | No | Yes |
回傳條件過濾後的特定值,可用於 text、number、 、date 欄位。支持 array 。
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
"query": "parks",
"filters" : {
"world_heritage_site": [ "true" ]
}
}'
array 應用
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
"query": "花蓮",
"filters" : {
"area": [ "吉安鄉", "壽豐鄉" ]
}
}'
回傳範圍內的條件過濾值,可用於 number 、 date 欄位
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d'{
"query": "park",
"filters": {
"date_established": {
"from": "1900-01-01T12:00:00+00:00",
"to": "1950-01-01T00:00:00+00:00"
}
}
}'
回傳相對位置條件過濾後的結果
用 distance 來代表方圓範圍內
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d'{
"query": "parks",
"filters": {
"location": {
"center": "37.386483, -122.083842",
"distance": 300,
"unit": "km"
}
}
}'
用 form、to,來代表距離從哪裡到哪裡
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d'{
"query": "parks",
"filters": {
"location": {
"center": "37.386483, -122.083842",
"unit": "m",
"from": 0,
"to": 1000
}
}
}'
還記得前面有提到的,Filter:any、all、none的設定嗎~,利用 API 能更靈活運用喔!
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
"query": "parks",
"filters": {
"all": [
{ "states": "California" },
{ "world_heritage_site": "true" }
],
"any": [
{ "acres": { "from": 40000 } },
{ "square_km": { "from": 500 } }
],
"none": [
{ "title": "Yosemite" }
]
}
}'
array 代表 "OR"
當建立 filter 物件,當中可以使用any、all、none三種過濾選項,以下使用 all 過濾選項並給予兩個 array。
下列例子代表 : ( "Washington" OR "Idaho" ) AND "world_heritage_site"
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
"query": "parks",
"filters": {
"all": [
{ "states": ["Washington", "Idaho"] },
{ "world_heritage_site": ["true"] }
]
}'
如果沒有寫任何過濾選項,直接用array也代表 "OR"
"filters": {
"states": ["Washington", "Idaho"]
}
Array and Object
當建立大型的條件過濾查詢,要注意條件過濾器只能建立32個,所以如果同一個欄位的值要建立多個條件過濾,可以運用array來替代object
賴可利私 :
Arrays: Any
你可以在 Any 參數下堆疊很多 objects
"filters" : {
"any": [
{ "sku": 1 },
{ "sku": 2 },
{ "sku": 3 },
{ "sku": 4 },
{ "sku": 5 },
{ "sku": 6 }
...
...
]
}
也可以用Array來替代
"filters" : {
"any": [
"sku": [1, 2, 3, 4, 5, 6 ...]
]
}
Arrays: All, None
如果在 all 參數下使用 array
等於在 all 之下崁入 any 選項
"filters" : {
"all": [
"sku": [1, 2, 3, 4, 5, 6 ...]
]
}
與以下寫法相同,代表值是 "OR"
"filters" : {
"all": {
"any": [
{ "sku": 1 },
{ "sku": 2 },
{ "sku": 3 },
{ "sku": 4 },
{ "sku": 5 },
{ "sku": 6 }
]
}
}
條件過濾選項可以像上面例子一樣互相套崁運用,但最多只能套崁五層
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
"query": "parks",
"filters": {
"any": [
{
"all": [
{ "states": "California" },
{ "world_heritage_site": "true" }
]
}
]
}
}'
感謝各位閱讀!今天就先到這邊拉~祝福大家都有美好的一天,又要放連假啦!真是太棒了,明天見掰掰~